/**
 * 
 */
package com.itboye.allshengmedical.sci.packet.resp;

import java.io.Serializable;

import android.util.Log;

import com.itboye.allshengmedical.sci.cmd.req.CmdType;

/**
 * 返回数据的基类
 * 
 * @author 贝贝 346551990@qq.com
 * @date 2016-12-9
 */
public class BaseRespPacket implements Serializable {

	// 期望数据长度
	private int expectDataLength;

	public int getExpectDataLength() {
		return expectDataLength;
	}

	public void setExpectDataLength(int expectDataLength) {
		this.expectDataLength = expectDataLength;
	}

	/**
	 * 是否合法内容长度
	 * 
	 * @return
	 */
	protected boolean isLegalContent() {
		return this.isLegalContent(getExpectDataLength());
	}

	/**
	 * 是否合法内容长度
	 * 
	 * @param len
	 * @return
	 */
	protected boolean isLegalContent(int len) {
		byte[] content = this.getContent();

		if (content != null && content.length >= len) {
			return true;
		}

		return false;
	}

	/**
	 * 
	 * @param packet
	 */
	protected void clone(BaseRespPacket packet) {
		this.setCmdType(packet.getCmdType());
		this.setCheckBit(packet.getCheckBit());
		this.setContent(packet.getContent());
		this.setEnd(packet.getEnd());
		this.setLen(packet.getLen());
		this.setStart(packet.getStart());
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -2990665363112748638L;

	/**
	 * 转换成bytes
	 * 
	 * @return
	 */
	public byte[] toBytes() {
		byte[] bytes = new byte[start.length + end.length + 1 + content.length
				+ checkBit.length];
		int bytesIndex = 0;
		System.arraycopy(start, 0, bytes, bytesIndex, start.length);
		bytes[start.length] = len;
		bytesIndex += start.length + 1;
		System.arraycopy(content, 0, bytes, bytesIndex, content.length);
		bytesIndex += content.length;
		System.arraycopy(checkBit, 0, bytes, bytesIndex, checkBit.length);
		bytesIndex += checkBit.length;
		System.arraycopy(end, 0, bytes, bytesIndex, end.length);

		return bytes;
	}

	// 例子数据

	// start len content checkBit end
	// ↓↓ ↓↓ ↓↓ ↓↓ ↓↓
	// 5AA5 04 01000000 0102 3EE3

	@Override
	public String toString() {
		return bytesToHexString(start) + bytesToHexString(len)
				+ bytesToHexString(content) + bytesToHexString(checkBit)
				+ bytesToHexString(end);
	}

	protected String bytesToHexString(byte src) {

		StringBuilder stringBuilder = new StringBuilder("");
		int v = src & 0xFF;
		String hv = Integer.toHexString(v);
		if (hv.length() < 2) {
			stringBuilder.append(0);
		}

		return stringBuilder.append(hv).toString();
	}

	protected String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}

		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}

		return stringBuilder.toString();
	}

	private byte[] start;// 起始字符5AA5
	private byte[] end;// 结束字符 3EE3
	private byte len;// 数据长度 0xFF
	private byte[] content;// 数据读取的内容
	private byte[] checkBit;// 校验位
	private CmdType cmdType;// 数据包类型=》根据数据内容第一个字节生成

	public CmdType getCmdType() {
		return cmdType;
	}

	public void setCmdType(CmdType cmdType) {
		this.cmdType = cmdType;
	}

	public void setCheckBit(byte[] checkBit) {
		this.checkBit = checkBit;
	}

	/**
	 * 求和
	 * 
	 * @return
	 */
	public int sumLenAndContent() {
		int sum = 0 + (this.len & 0xff) ;

		for (int i = 0; i < this.content.length; i++) {
			sum += (int)( this.content[i] & 0xff);
		}

		return sum;
	}

	/**
	 * 验证校验位
	 * 
	 * @return
	 */
	public boolean isValidPack() {

		if (this.checkBit == null) {
			return false;
		}
		int sum = this.sumLenAndContent();
		System.out.println("checkBit= " + checkBit.length); 
		int sumCompare =( checkBit[1] & 0xFF) + ( 256 * (checkBit[0]  & 0xFF));
		System.out.println("sum= " + sum + "sumCompare= " + sumCompare);
		
		return sum == sumCompare;
	}


	public byte[] getStart() {
		return start;
	}

	public void setStart(byte[] start) {
		this.start = start;
	}

	public byte[] getEnd() {
		return end;
	}

	public void setEnd(byte[] end) {
		this.end = end;
	}

	public byte getLen() {
		return len;
	}

	public void setLen(byte len) {
		this.len = len;
	}

	/**
	 * 获取十六进制的一位内容
	 * 
	 * @param index
	 * @return
	 */
	public String getHexContent(int index) {
		return bytesToHexString(getContent(index));
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public byte getContent(int index) {

		if (this.content != null && this.content.length > index
				&& this.content.length == (int) this.len) {
			return content[index];
		}

		return 0;
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
		if (this.content != null && this.content.length > 1) {
			cmdType = CmdType.getCmdType(content[0]);
		}
	}

	public byte[] getCheckBit() {
		return checkBit;
	}

}
